package MyDaily.Servlet;

import java.io.IOException;
import java.sql.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import MyDaily.dao.user.InsertUser;
import MyDaily.pojo.User;

/**
 * Servlet implementation class InsertServlet
 */
@WebServlet("/RegisterServlet")
public class RegisterServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public RegisterServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// 设置请求和响应数据的字符集编码
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		// 获取页面用户输入的信息
		String phone = request.getParameter("phone");
		String name = request.getParameter("name");
		String sex = request.getParameter("sex");
		String birthday = request.getParameter("birthday");
		Date date = Date.valueOf(birthday);
		String password = request.getParameter("password");
		// 将页面信息添加到User中
		User user = new User();
		user.setPhone(phone);
		user.setName(name);
		user.setSex(sex);
		user.setBirthday(date);
		user.setPassword(password);
		// 实例化新增
		InsertUser insertUser = new InsertUser();
		// 调用增加用户的方法
		int row = insertUser.Insertuser(user);
		if (row > 0) {
			// 转发至登录界面
			request.getRequestDispatcher("login.jsp").forward(request, response);
		} else {
			request.setAttribute("msg", "对不起,注册用户失败");
			request.getRequestDispatcher("register.jsp").forward(request, response);
		}
	}

}
